﻿using Microsoft.AspNetCore.Mvc;
using PMS.Core.Caching;
using PMS.Core.Configuration.CommonEnumConfigs;
using PMS.Core.Infrastructure;
using PMS.Data.Entities.Settings;
using PMS.Services.Caching;
using PMS.Services.EntityServices.NaviMenus;
using PMS.Services.EntityServices.Security;
using PMS.Services.EntityServices.Stores;
using PMS.Services.SharedKernel;
using PMS.Web.Factories.NaviMenus;
using PMS.Web.Factories.SystemModules;
using PMS.Web.Framework.Controllers;
using PMS.Web.Framework.Mvc.Filters;
using PMS.Web.Models.NaviMenus;

namespace PMS.Web.Controllers.Backend.ArticlesManage
{
    public class ArticlesManageController : BaseAdminController
    {
        #region Fields
        private readonly IAclService _aclService;
        private readonly IWorkContext _workContext;
        private readonly IStoreService _storeService;
        private readonly ICacheKeyService _cacheKeyService;
        private readonly IStaticCacheManager _staticCacheManager;
        private readonly INaviMenuService _naviMenuService;
        private readonly IStoreMappingService _storeMappingService;
        private readonly INaviMenuModelFactory _naviMenuModelFactory;
        private readonly ISystemModuleModleFactory _systemModuleFactory;
        #endregion

        #region Ctor
        public ArticlesManageController(IAclService aclService,
            IWorkContext workContext,
            IStoreService storeService,
            ICacheKeyService cacheKeyService,
            IStaticCacheManager staticCacheManager,
            INaviMenuService naviMenuService,
            IStoreMappingService storeMappingService,
            INaviMenuModelFactory naviMenuModelFactory,
            ISystemModuleModleFactory systemModuleModleFactory)
        {
            _aclService = aclService;
            _workContext = workContext;
            _storeService = storeService;
            _cacheKeyService = cacheKeyService;
            _staticCacheManager = staticCacheManager;
            _naviMenuService = naviMenuService;
            _storeMappingService = storeMappingService;
            _naviMenuModelFactory = naviMenuModelFactory;
            _systemModuleFactory = systemModuleModleFactory;
        }
        #endregion

        #region Methods

        #region Article_NaviMenu
        [ModulePermission]
        public IActionResult NaviMenuListView(string hashId)
        {
            int moduleId = DecodeId(hashId);
            var model = _systemModuleFactory.GetSystemModuleModel(moduleId);
            ViewBag.ModuleId = model.Id;
            ViewBag.PageTitle = model.DisplayName;
            ViewBag.BodyHtmlClass = "pear-container";
            //prepare model
            var searchModel = new NaviMenuSearchModel
            {
                Group = MenuGroups.Articles,
                LanguageCode = _workContext.WorkingLanguage.LanguageCode
            };
            ViewBag.SearchModel = _naviMenuModelFactory.PrepareNaviMenuSearchModel(searchModel, _workContext.CurrentCustomer);
            return View(new NaviMenuEditModel());
        }

        /// <summary>
        /// 文章/新闻模块导航菜单详情编辑
        /// </summary>
        /// <param name="buttonId">按钮权限验证参数：点击按钮Id</param>
        /// <param name="moduleId">按钮权限验证参数：按钮所在系统模块Id</param>
        /// <param name="menuId">数据加载Id, 0则为新增数据</param>
        /// <returns></returns>
        [ButtonPermission]
        public virtual IActionResult NaviMenuDetailEditView(int buttonId, int moduleId, int menuId)
        {
            ViewBag.PageTitle = "菜单详情编辑";
            var menuEntity = _naviMenuService.GetNaviMenuById(menuId);
            NaviMenuEditModel entityModel = menuEntity == null
                ? new NaviMenuEditModel //For new MenuItem insert
                {
                    LanguageCode = _workContext.WorkingLanguage.LanguageCode,
                    MenuGroup = MenuGroups.Articles
                } 
                : null; //For MenuItem edit
            NaviMenuEditModel model = _naviMenuModelFactory.PrepareNaviMenuEditModel(entityModel, menuEntity);

            var mediaSettings = EngineContext.Current.Resolve<MediaSettings>();
            GetMediaFileUploadLimitations(mediaSettings, MediaFileType.Image, 1, MediaFileUseage.Article);
            return View(model);
        }
        #endregion
        #endregion
    }
}
